<?php
namespace Home\Controller;
use Common\Controller\CommonController;

/**
 * 维修工类
 * @author 张亚伟 <zhangyw@zhijin101.com>
 */
class MaintainerController extends CommonController
{
    public function index()
    {
        $model = M('Maintainer');
		$Count = $model->count();
		$Page = new \Think\Pager($Count,10);
		$show = $Page->showhtml();
		$keyword = I('keyword');
        $where = ['check_status'=>1];
		if(!empty($keyword)){
			$where['name|mobile'] = array('like',"%".$keyword."%");
		}
        if ($area = I('area')) {
            $where['province'] = explode('/', $area)['0'];
            $where['city'] = explode('/', $area)['1'];
            $where['district'] = explode('/', $area)['2'];
            $this->assign('areaArr',$area);
        }

		$list = $model->where($where)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
		if($list){
            $category = yzl_config('wxg.wxg_category');
			foreach($list as $key=>&$row){
				$row['status'] = $row['status'] ? '开启' : '关闭';
                $row['category'] = isset($category[$row['category_id']]) ? $category[$row['category_id']] : '';
			}
        }
		$this->assign('page',$show);
		$this->assign('list', $list);
		$this->display();
    }

    public function add()
    {
		if(IS_POST){
			$data = I('post.');
            if(empty($data['category_id'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请选择分类'));
            }
            if(empty($data['name'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入维修工姓名'));
            }
            if(empty($data['img'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入上传图片'));
            }
            if(empty($data['mobile'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入联系电话'));
            }
            // if(empty($data['pcd'])){
            //     $this->ajaxReturn(array('s'=>false,'m'=>'请选择地区'));
            // }
            $pcd = explode("/", $data['pcd']);
            if(empty($pcd[0])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请选择省份'));
            }
            if(empty($pcd[1])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请选择城市'));
            }
            // if(empty($pcd[2])){
            //     $this->ajaxReturn(array('s'=>false,'m'=>'请选择地区'));
            // }
            if(empty($data['address'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入详细地址'));
            }
            $data['img'] = isset($data['img'][0]) ? $data['img'][0] : '';
            $data['province'] = $pcd[0];
            $data['city'] = $pcd[1];
            $data['district'] = $pcd[2];
            $data['check_status'] = 1;

            $model = M('Maintainer');

            $user_model = M('user');
            $user = $user_model->where(['phone'=>$data['mobile']])->find();
            if($user){
                $user_id = $user['id'];
            }else{
                $user_id = user_reg([
                    'img'       => C('SiteUrl').'Public/upload/userimg/t.jpg',
                    'nickname'  => "用户".$data['mobile'],
                    'phone'     => $data['mobile'],
                    'pwd'       => md5(123456),
                    'ctime'     => time(),
                ]);
                if(!$user_id){
                    $this->ajaxReturn(array('s'=>false,'m'=>'用户添加失败!'));
                }
            }
            $data['uid'] = $user_id;
            $data['create_time'] = time();

			$id = $model->add($data);
			if($id){
				$this->ajaxReturn(array('s'=>true,'m'=>'添加成功', 'url'=>U('index')));
			}else{
				$this->ajaxReturn(array('s'=>false,'m'=>'添加失败!'));
			}
		}else{
            $advertising = M('Article_advertising')->select();
            $this->assign('advertising', $advertising);
			$this->assign('category', yzl_config('wxg.wxg_category'));
			$this->display();
		}
    }

    public function edit()
    {
		$id = I('id');
        if(IS_POST){
			$data = I('post.');
            if(empty($data['category_id'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请选择分类'));
            }
            if(empty($data['name'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入维修工姓名'));
            }
            if(empty($data['img'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入上传图片'));
            }
            if(empty($data['mobile'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入联系电话'));
            }
            // if(empty($data['pcd'])){
            //     $this->ajaxReturn(array('s'=>false,'m'=>'请选择地区'));
            // }
            $pcd = explode("/", $data['pcd']);
            if(empty($pcd[0])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请选择省份'));
            }
            if(empty($pcd[1])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请选择城市'));
            }
            // if(empty($pcd[2])){
            //     $this->ajaxReturn(array('s'=>false,'m'=>'请选择地区'));
            // }
            if(empty($data['address'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入详细地址'));
            }
            $data['img'] = isset($data['img'][0]) ? $data['img'][0] : '';
            $data['province'] = $pcd[0];
            $data['city'] = $pcd[1];
            $data['district'] = $pcd[2];

            $model = M('Maintainer');

			$res = $model->where(['id'=>$id])->save($data);
			if($res!==false){
				$this->ajaxReturn(array('s'=>true,'m'=>'修改成功', 'url'=>U('index')));
			}else{
				$this->ajaxReturn(array('s'=>false,'m'=>'修改失败!'));
			}
		}else{
			$info = M('Maintainer')->where(['id'=>$id])->find();
            $advertising = M('Article_advertising')->select();
            $this->assign('advertising', $advertising);
			$this->assign('info', $info);
			$this->assign('category', yzl_config('wxg.wxg_category'));
			$this->display();
		}
    }
    
    public function apply(){
        $model = M('Maintainer');
		$Count = $model->count();
		$Page = new \Think\Pager($Count,10);
		$show = $Page->showhtml();
		$keyword = I('keyword');
        $where = ['check_status'=>['in', [0]]];
		if(!empty($keyword)){
			$where['name|mobile'] = array('like',"%".$keyword."%");
		}

		$list = $model->where($where)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
		if($list){
            $status_arr = ['申请中', '同意', '拒绝'];
			foreach($list as $key=>&$row){
				$row['status'] = $status_arr[$row['check_status']];
			}
		}
		$this->assign('page',$show);
		$this->assign('list', $list);
		$this->display();
    }

    public function apply_edit(){
        $id = I('id');
        if(IS_POST){
			$data = I('post.');
            if(empty($data['name'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入维修工姓名'));
            }
            if(empty($data['img'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入上传图片'));
            }
            if(empty($data['mobile'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入联系电话'));
            }
            // if(empty($data['pcd'])){
            //     $this->ajaxReturn(array('s'=>false,'m'=>'请选择地区'));
            // }
            $pcd = explode("/", $data['pcd']);
            if(empty($pcd[0])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请选择省份'));
            }
            if(empty($pcd[1])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请选择城市'));
            }
            // if(empty($pcd[2])){
            //     $this->ajaxReturn(array('s'=>false,'m'=>'请选择地区'));
            // }
            if(empty($data['address'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入详细地址'));
            }
            $data['img'] = isset($data['img'][0]) ? $data['img'][0] : '';
            $data['province'] = $pcd[0];
            $data['city'] = $pcd[1];
            $data['district'] = $pcd[2];
            if($data['check_status']){
                $data['check_time'] = time();
                if($data['check_status'] == 1){
                    $data['status'] = 1;
                }
            }
            $model = M('Maintainer');

			$res = $model->where(['id'=>$id])->save($data);
			if($res!==false){
				$this->ajaxReturn(array('s'=>true,'m'=>'修改成功', 'url'=>U('apply')));
			}else{
				$this->ajaxReturn(array('s'=>false,'m'=>'修改失败!'));
			}
		}else{
			$info = M('Maintainer')->where(['id'=>$id])->find();
			$this->assign('info', $info);
			$this->display();
		}
    }

    public function del(){
        $id = I('id');
        $res = M('Maintainer')->where(['id'=>$id])->delete();
        if($res){
            $this->ajaxReturn(array('s'=>true,'m'=>'删除成功', 'url'=>U('index')));
        }else{
            $this->ajaxReturn(array('s'=>false,'m'=>'删除失败!'));
        }
    }
}
